package com.sany.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sany.web.session.WebSession;
public class PortalValidate extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	private static final long serialVersionUID = 1L;
	public PortalValidate() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 Cookie[] all_Cookies = request.getCookies();
         String ticket = "";
         int i = 0;
         if (all_Cookies == null) {
             System.out.println("all_Cookies==null");
         } else {
             for (i = 0; i < all_Cookies.length; i++) {
                 if ("MYSAPSSO2".equals(all_Cookies[i].getName())) {
                     ticket = all_Cookies[i].getValue();
                     System.out.println(ticket);
                     ticket = (java.net.URLDecoder.decode(ticket, "utf-8")).trim();   
                                      
                     break;
                 }
             }
         }
         String Path = request.getScheme() + "://"+ request.getServerName() + ":"+ request.getServerPort() + request.getContextPath() + "/";
			     
         
         if ("".equals(ticket)) {            
         
				String pageURl = Path + "LogonAction.shtml?method=execute_noLogin&josso_username=default";
				response.sendRedirect(pageURl);
				 //System.out.println(">>>>>>>>>ticket is empty");   
        } else {
       //  System.out.println(">>>>>>>>>ticket is getted"); 
        System.out.println("0----------========SSO2Ticket");
        //  SSO2Ticket ticketParse = new SSO2Ticket();
          //ticketParse.parseTicket(ticket);
          WebSession websession = new WebSession();
          websession.setUserName("liujia");
		 request.getSession().setAttribute("user", websession);         
          //session.setAttribute("checkinPortal","true");  
          String pageURl = Path + "LogonAction.do?method=execute_Login&josso_username=liujia";
		  response.sendRedirect(pageURl);
         }
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
